let Cesium:any=undefined
/**
 *动态泛光线(随机颜色)
 * @global
 * @param {object} options
 * @param {Color} options.color - 颜色
 * @param {number} options.duration -持续时长
 * @param {string} options.imgUrl - 图片
 * @see {@link module:Graphics#getLineGraphics|getLineGraphics}
 * @example
 * import { Graphics } from 'cesium_dev_kit'
 * const graphicObj = new Graphics({
 *     cesiumGlobal: Cesium,
 *     containerId: 'cesiumContainer'
 * })
 * const  polylineCityLinkMaterial = new Cesium.Scene.PolylineCityLinkMaterialProperty({
            color: new Cesium.Color(5.0, 5.0, 10.0),
            duration: 2000
          });
 * graphicObj.graphics.getLineGraphics({
 *     positions:Cesium.Cartesian3.fromDegreesArray([
          104.09816110606057,
          30.659821965447698,
          104.1120972824757,
          30.65330300319938,
          104.10758419863926,
          30.64592470850288,
          104.09351691196979,
          30.652434826507452,
          104.09816110606057,
          30.659821965447698
        ]),
 *      material:polylineCityLinkMaterial,
 *      clampToGround:false,
 *      width:1
 * })
 */
export class PolylineCityLinkMaterialProperty extends Cesium.Property {
  private  _definitionChanged: any;
   private _color: any;
   private _colorSubscription: any;
   private  _time: any;
  private  _img: any;
   private _duration:any;
    constructor(options: any) {
        super()
        options = Cesium.defaultValue(options, Cesium.defaultValue.EMPTY_OBJECT);
        this._definitionChanged = new Cesium.Event();

        this._colorSubscription = undefined;
        this._color = options.color || Cesium.Color.BLUE;
        this._duration = options.duration || 1000;
        this._time = undefined;
        this._img = options.imgUrl || Cesium.Material.PolylineCityLinkImage;
    }

     get isvarant() {
        return false;
     }
    get definitionChanged() {
        return this._definitionChanged;
    }

    get color() {
        return this._color;
    }

    set color(value: any) {
        this._color = value;
    }

    getType() { // 'PolylineCityLink'
        // @ts-ignore
        return   Cesium.Material.PolylineCityLinkType;
    }

    getValue(time: any, result: any) {
        if (!Cesium.defined(result)) {
            result = {};
        }
        result.color = Cesium.Property.getValueOrClonedDefault(this._color, time, Cesium.Color.WHITE, result.color);
        result.image = this._img; //Material.PolylineCityLinkImage
        if (this._time === undefined) {
            this._time = time.secondsOfDay;
        }
        result.time = ((time.secondsOfDay - this._time) * 1000) / this.duration;
        return result;
    }

    equals(other: any) {
        return (
            this === other || //
            (other instanceof PolylineCityLinkMaterialProperty && Cesium.Property.equals(this._color, other._color))
        );
    }

}
// 动态泛光线
function _getDynamicLightLineShader (options:any) {
    if (options && options.get) {
        return "czm_material czm_getMaterial(czm_materialInput materialInput)\n\
              {\n\
                  czm_material material = czm_getDefaultMaterial(materialInput);\n\
                  vec2 st = materialInput.st;\n\
                  \n\
                  vec4 colorImage = texture(image, vec2(fract(1.0 *st.s - time), fract(st.t)));\n\
                  \n\
                  vec4 fragColor;\n\
                  fragColor.rgb = (colorImage.rgb+color.rgb) / 1.0;\n\
                  fragColor = czm_gammaCorrect(fragColor);\n\
                  material.diffuse = colorImage.rgb;\n\
                  material.alpha = colorImage.a;\n\
                  material.emission = fragColor.rgb;\n\
                  \n\
                  return material;\n\
              }\n\
              ";
        // material.diffuse = max(color.rgb * material.alpha * 3.0, color.rgb);\n\
        // material.alpha = texture2D(image, vec2(1.0 - fract(time - st.s), st.t)).a * color.a;\n\
    }
}

function getDfSt(dfSt:any,attrArr:any) {
    if (dfSt) {
        const res = attrArr.reduce((pre:any, curr:any) => {
            if (pre) {
                return pre[curr];
            } else {
                return dfSt[curr];
            }
        }, undefined);
        return dfSt.baseService + res;
    }
    return undefined;
}


const  _installCityLineMaterial=(dfSt:any,CesiumSources:any)=> {
    Cesium=CesiumSources
    // @ts-ignore
    Cesium.Material.PolylineCityLinkType = "PolylineCityLink";
    // @ts-ignore
    Cesium.Scene.PolylineCityLinkMaterialProperty = PolylineCityLinkMaterialProperty;
    // @ts-ignore
    Cesium.Material.PolylineCityLinkImage =
        getDfSt(dfSt,["material", "PolylineCityLinkMaterialProperty"]) || "static/data/images/Textures/meteor_01.png";
    // @ts-ignore
    Cesium.Material._materialCache.addMaterial(Cesium.Material.PolylineCityLinkType, {
    fabric: {
        // @ts-ignore
        type: Cesium.Material.PolylineCityLinkType,
        uniforms: {
            color: new Cesium.Color(1, 0, 0, 1.0),
            // @ts-ignore
            image: Cesium.Material.PolylineCityLinkImage,
            time: 0,
        },
        source: _getDynamicLightLineShader({
                                               get: true,
                                           }),
    },
    translucent: function () {
    return true;
},
});
}

export  {
    _installCityLineMaterial
};

